package main

import (
	"log"
	"os"
	"os/signal"
	"syscall"

	"server/src/zyplog"
)

const VER string = "数据推送服务器 v3.1 build 230830"

func main() {
	var err error
	var targetName string

	initGlobal()

	cfg, err := loadConfig("")
	if err != nil {
		log.Println(VER, err.Error())
		return
	}

	var logType zyplog.LogType
	switch cfg.Log.Type {
	case 0:
		logType = zyplog.LOG_STDOUT
	case 1:
		logType = zyplog.LOG_FILE
	case 2:
		logType = zyplog.LOG_SYSLOG
	}
	g_log, err = zyplog.NewZypLoger(logType, cfg.Log.Path)
	if err != nil {
		log.Println("创建日志失败：", err.Error())
		return
	}

	targetName = cfg.CompanyName

	zLog(targetName, VER, " 启动")

	dbo, err := NewDBOperation(cfg)
	if err != nil {
		zLogError("数据库连接失败，程序退出:", err)
		return
	} else {
		zLog("数据库连接成功")
	}
	defer dbo.Close()

	NewProcessor(dbo, cfg).Run()

	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

	s := <-signalChan
	zLog(targetName, VER, "收到退出信号:", s)
}
